interface NameList {
  RECORDS: Array<{ nickname: string }>;
}
/**
 * 封装处理用户名部分隐藏函数
 */
export const nameCarousel = (params: NameList) => {
  let str =
    params.RECORDS[parseInt((Math.random() * 3000).toString(), 10)].nickname;
  const w1 = str[0] || "";
  const w2 = str[2] || "";
  const w3 = str[str.length - 1] || "";
  const w4 = str[str.length] || "";
  const strFront = `${w1}${w2}`;
  const strBack = `${w3}${w4}`;
  str = `${strFront}***${strBack}`;
  return str;
};

export function parseTime(time: number | string, cFormat?: string) {
  if (arguments.length === 0) {
    return null;
  }
  const format = cFormat || "{y}-{m}-{d} {h}:{i}:{s}";
  let date;
  if (typeof time === "object") {
    date = time;
  } else {
    if (typeof time === "string" && /^[0-9]+$/.test(time)) {
      time = parseInt(time, 10);
    }
    if (typeof time === "number" && time.toString().length === 10) {
      time = time * 1000;
    }
    date = new Date(time);
  }
  const formatObj: any = {
    y: date.getFullYear(),
    m: date.getMonth() + 1,
    d: date.getDate(),
    h: date.getHours(),
    i: date.getMinutes(),
    s: date.getSeconds(),
    a: date.getDay(),
  };
  const timeStr: string = format.replace(/{([ymdhisa])+}/g, (result, key) => {
    const value = formatObj[key];
    // Note: getDay() returns 0 on Sunday
    if (key === "a") {
      return ["日", "一", "二", "三", "四", "五", "六"][value];
    }
    return value.toString().padStart(2, "0");
  });
  return timeStr;
}

export function getCurrentTime(time: number) {
  if (arguments.length === 0) {
    return null;
  }
  if (typeof time === "string" && /^[0-9]+$/.test(time)) {
    time = parseInt(time, 10);
  }
  const currentTime: any = {
    d: Math.floor(time / (1000 * 60 * 60 * 24)),
    h: Math.floor((time % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)),
    m: Math.floor((time % (1000 * 60 * 60)) / (1000 * 60)),
    s: Math.floor((time % (1000 * 60)) / 1000),
  };
  for (const key in currentTime) {
    if (currentTime.hasOwnProperty(key)) {
      const element = currentTime[key];
      if (element < 10) {
        currentTime[key] = "0" + currentTime[key];
      }
    }
  }
  return currentTime;
}

